<?php

/**
 * MyProjectNameHere <http://www.example.com>
 * Model Class
 *
 * It is recommended to extend Model classes from WWW_Factory in order to 
 * provide various useful functions and API access for the Model.
 *
 * @package    Factory
 * @author     DeveloperNameHere <email@example.com>
 * @copyright  Copyright (c) 2012, ProjectOwnerNameHere
 * @license    Unrestricted
 * @tutorial   /doc/pages/guide_mvc.htm
 * @since      1.0.0
 * @version    1.0.0
 */

class WWW_model_business extends WWW_Factory {

	/**
	 * It is recommended to define all data variables here. Usually the 
	 * data variables have the same names as the column names of database 
	 * rows from a table.
	 */	
	public $conn;
	/**
	 * Alternative to __construct()
	 *
	 * WWW_Factory does not allow to overwrite __construct() method, so 
	 * this __initialize() is used instead and loaded automatically when 
	 * object is created.
	 *
	 * @return boolean
	 */
	public function __initialize()
	{
		require('engine/class.www-database.php');
		$this->conn=new WWW_Database();				
		return true;
		
	}
	public function create_category($data)
	{
	$query="insert into `businesses_category` (admin_id,category_name,category_content,created_date,updated_date) VALUES ('".$data['admin_id']."','".$data['category_name']."','".$data['category_content']."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
	$result=$this->conn->dbCommand($query);	
		if($result){
			return array('message'=>'Category successfully created');			
		}else{
			return array('message'=>'Category Creation Failed');
		}	
	}
	public function edit_category($data)
	{
	$query="update `businesses_category` set category_name='".$data['category_name']."',category_content='".$data['category_content']."',updated_date='".date("Y-m-d H:i:s")."' where businesscat_id=".$data['businesscat_id'];
		//print_r($query);exit;
		$result=$this->conn->dbCommand($query);		
		if($result){
			return array('message'=>'Updated Successfully');			
		}else{
			return array('message'=>'Updates Failed');
		}	
	
	}
	public function get_category($data)
	{
		$query="SELECT bc.*, au.user_id,au.admin_id from businesses_category bc
 left join admin_users au on bc.admin_id=au.admin_id  WHERE businesscat_id='".$data['businesscat_id']."'";
		//print_r($query);exit;
		$result=$this->conn->dbMultiple($query);		
		if($result){
			return array('message'=>'Category type data','result'=>$result);		
		}else{
			return array('message'=>'Category by id is failed');
		}	
	
	}
	public function delete_category($data)
	{
		$query="update `businesses_category` set status='".$data['status']."' where businesscat_id='".$data['businesscat_id']."'";
		$data=$this->conn->dbCommand($query);
		if($data)
			return array('message'=>"Category status changed Successfully");		
		else
			return array('message'=>'Category status changed failed');
	}
	public function all_category($data){
		
	$current_page=0;
	$results_per_page=10;
	$order_by="created_date";
	$sort_order="desc";
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		$order_by=$data['order_by'];
	}
	if(isset($data['sort_order']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
	$query_string="SELECT bc.*, au.user_id,au.admin_id from businesses_category bc
 left join admin_users au on bc.admin_id=au.admin_id";
	$query_string1="select count(*) as total from businesses_category";
	if (isset($data['search_by']) && $data['search_by']!='')
	{	//print_r($data);exit;
		
		if($data['search_by'] == 'category_name')
		{	
			$search_by = "category_name";
			
		}
		if($data['search_by'] == 'admin_id')
		{
			$search_by = "au.user_id";
			
		}
		if($data['search_by'] == 'created_date')
		{
			$search_by = "bc.created_date";
		}
		$query = $query_string." where ".$search_by." like '%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		$query1 = $query_string1." where ".$search_by." like '%".$data['search_by_value']."%'";
		
	}else
	{
		$query=$query_string." order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		$query1 = $query_string1;
	}	
		//print_r($query1);exit;
		$result=$this->conn->dbMultiple($query);
		if($result){
			$totalrows = $this->conn->dbSingle($query1);
		return array('message'=>"All ","info"=>$result,'total'=>$totalrows['total'],'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No business categories');
		
	}
	public function create_business($data)
	{
			
			$file = $data['business_image']['tmp_name'];
			$filename = time().$data["business_image"]['name'];
			$business_image_path="uploads/business/".$filename;
			move_uploaded_file($file, $banner_image_path);
			
	
	if($data['business_type']=="premium")
	{
		
		$expire_in = 30;
		
	}else if($data['business_type']=="standard")
	{
		$expire_in = 15;
	}
		
	$query="insert into `businesses` (business_name,businesscat_id,member,business_type,start_date,description,url,address,business_image,image_name,expire_in,created_date,updated_date) VALUES ('".$data['business_name']."','".$data['businesscat_id']."','".$data['member']."','".$data['business_type']."','".$data['start_date']."','".$data['description']."','".$data['url']."','".$data['address']."','".$this->conn->baseurl().$business_image_path."','".$data['image_name']."','".$expire_in."','".date("Y-m-d H:i:s")."','".date("Y-m-d H:i:s")."')";
		//print_r($query);exit;
	$result=$this->conn->dbCommand($query);	
		//$this->db->last_query();exit;
	if($result){
			return array('message'=>'businesses successfully created');			
		}else{
			return array('message'=>'businesses Creation Failed');
		}	
	}
	public function get_businesscategory_list()
	{	
		$query = "select businesscat_id,category_name from businesses_category";
		$result = $this->conn->dbMultiple($query);		
		if($result){
			return array('message'=>'Category type data','result'=>$result);		
		}else{
			return array('message'=>'Category by id is failed');
		}
	}
	public function edit_business($data)
	{	
		if($data['business_type']=="premium")
	{
		
		$expire_in = 30;
		
	}else if($data['business_type']=="standard")
	{
		$expire_in = 15;
	}
		
		if($data['business_image']!="")
			{
			$file = $data['business_image']['tmp_name'];
			$filename = time().$data["business_image"]['name'];
			$business_image_path="uploads/business/".$filename;
			move_uploaded_file($file, $banner_image_path);
			}
			else
			{
				$business_image_path = "";
			}
		
		
			$query="update `businesses` set business_name='".$data['business_name']."',businesscat_id='".$data['businesscat_id']."',member='".$data['member']."',business_type='".$data['business_type']."',start_date='".$data['start_date']."',description='".$data['description']."',url='".$data['url']."',address='".$data['address']."'".($business_image_path!="" ? ',banner_image='.$this->conn->baseurl().$business_image_path : '').",image_name='".$data['image_name']."',expire_in='".$expire_in."',updated_date='".date("Y-m-d H:i:s")."' where business_id=".$data['business_id'];
		$result=$this->conn->dbCommand($query);		
		if($result){
			return array('message'=>'Updated Successfully');			
		}else{
			return array('message'=>'Updates Failed');
		}	
	
	}
	
	public function get_business($data)
	{
		//print_r($data);exit;
	$query="SELECT b.*,bc.businesscat_id,bc.category_name,au.admin_id,au.user_id FROM businesses b left join businesses_category bc on b.businesscat_id=bc.businesscat_id left join admin_users au on bc.admin_id=au.admin_id where business_id='".$data['business_id']."' and TIMESTAMPDIFF(DAY, start_date,'".date("Y-m-d H:i:s")."') < expire_in";
		//print_r($query);exit;
		$result=$this->conn->dbMultiple($query);		
		if($result){
			return array('message'=>'business type data','result'=>$result);			
		}else{
			return array('message'=>'business by id is failed');
		}	
	
	}
	public function delete_business($data)
	{
		$query="update `businesses` set status='".$data['status']."' where business_id='".$data['business_id']."'";
		$data=$this->conn->dbCommand($query);
		if($data)
			return array('message'=>"business Deleted Successfully");		
		else
			return array('message'=>'business Deletion Fails');
	}
	
	
	
	public function all_businesses($data)
	{	
	$current_page=0;
	$results_per_page=10;
	$order_by="b.created_date";
	$sort_order="desc";
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		$order_by=$data['order_by'];
	}
	if(isset($data['sort_order']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
	$query_string="SELECT b.*,bc.businesscat_id,bc.category_name,au.admin_id,au.user_id FROM businesses b left join businesses_category bc on b.businesscat_id=bc.businesscat_id left join admin_users au on bc.admin_id=au.admin_id";
	$query_string1="select count(*) as total FROM businesses b left join businesses_category bc on b.businesscat_id=bc.businesscat_id left join admin_users au on bc.admin_id=au.admin_id";
	if (isset($data['search_by']) && $data['search_by']!='')
	{	//print_r($data);exit;
		
		
		if($data['search_by'] == 'category_name')
		{
				$search_by = "bc.category_name";
		}else if($data['search_by'] == 'business_name')
		{
			$search_by = "business_name";
		}else if($data['search_by'] == 'created_date')
		{
			$search_by = "b.created_date";
		}
		$query =$query_string." where ".$search_by." like '%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		$query1 =$query_string1." where ".$search_by." like '%".$data['search_by_value']."%'";
		
	}else
	{
	$query=$query_string." order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
	$query1 = $query_string1;
	}	
		//print_r($query1);exit;
		$result=$this->conn->dbMultiple($query);
		if($result){
			$totalrows = $this->conn->dbSingle($query1);
		return array('message'=>"All ","info"=>$result,'total'=>$totalrows['total'],'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No business');
		
	}
}
	
?>